package com.csx.micrometer.threadpool.bean;

import com.csx.micrometer.threadpool.cache.ThreadPoolSpecialProCache;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author cuisongxu
 * @date 2022/11/21 15:12
 */
public class ThreadPoolBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof ThreadPoolExecutor) {
            ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) bean;
            // 记录下创建线程池的时间
            ThreadPoolSpecialProCache.ThreadPoolPro threadPoolPro = new ThreadPoolSpecialProCache.ThreadPoolPro();
            threadPoolPro.setThreadPoolName(beanName);
            threadPoolPro.setThreadPoolStartMillsTime(System.currentTimeMillis());
            threadPoolPro.setLastComputeCompletedTaskMillsTime(System.currentTimeMillis());
            ThreadPoolSpecialProCache.getInstance().getMap().put(beanName, threadPoolPro);
        }
        return bean;
    }
}
